﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using GPSTrackingWebsiteManager.ClassManager;

namespace GPSTrackingWebsiteManager.Pages
{
    public partial class GPSTrackingWebsiteManager : System.Web.UI.MasterPage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                SessionManager objSessionManager = (SessionManager)HttpContext.Current.Session["SessionManager"];

                if (objSessionManager != null)
                {
                    string[] Username;
                    string CompanyID = string.Empty;

                    //if (Request.Cookies["CompanyID"] != null) CompanyID = Request.Cookies["CompanyID"].Value;

                    //if (!string.IsNullOrEmpty(CompanyID))
                    //{
                    //    Username = HttpContext.Current.User.Identity.Name.ToString().Split('-');
                    //    Menu.Items[0].Text = "Bienvenido(a): " + Username[1];
                    //}
                    //else
                    //{
                    Menu.Items[0].Text = "Bienvenido(a): " + HttpContext.Current.User.Identity.Name;
                    //}

                    if (objSessionManager.IsMaster)
                    {
                        Menu.FindItemByValue("AdministratorUserMenu").Visible = true;

                        DataTable dtResult = GPSTrackingDBV2Custom.Data.DataRepository.MenuItemsProvider.GetByMasterUsername(HttpContext.Current.User.Identity.Name);

                        for (int index = 0; index < dtResult.Rows.Count; index++)
                        {
                            try
                            {
                                if (dtResult.Rows[index]["ValueOwner"] == null)
                                {
                                    Menu.FindItemByValue(dtResult.Rows[index]["Value"].ToString().Trim()).Text = dtResult.Rows[index]["Description"].ToString();
                                    Menu.FindItemByValue(dtResult.Rows[index]["Value"].ToString().Trim()).Visible = true;
                                }
                                else
                                {
                                    Menu.FindItemByValue(dtResult.Rows[index]["ValueOwner"].ToString().Trim()).Visible = true;
                                }
                            }
                            catch { }
                            try
                            {
                                Menu.FindItemByValue(dtResult.Rows[index]["Value"].ToString().Trim()).Visible = true;
                            }
                            catch { }
                        }
                    }
                    else
                    {
                        if (objSessionManager.IsAdmin)
                        {
                            Menu.FindItemByValue("AdministratorUserMenu").Visible = true;

                            DataTable dtResult = GPSTrackingDBV2Custom.Data.DataRepository.MenuItemsProvider.GetByAdminUsername(objSessionManager.CompanyID, HttpContext.Current.User.Identity.Name);

                            for (int index = 0; index < dtResult.Rows.Count; index++)
                            {
                                try
                                {
                                    //Menu.FindItemByValue(dtResult.Rows[index]["ValueOwner"].ToString().Trim()).Visible = true;
                                    if (dtResult.Rows[index]["ValueOwner"] == null)
                                    {
                                        Menu.FindItemByValue(dtResult.Rows[index]["Value"].ToString().Trim()).Text = dtResult.Rows[index]["Description"].ToString();
                                        Menu.FindItemByValue(dtResult.Rows[index]["Value"].ToString().Trim()).Visible = true;
                                    }
                                    else
                                    {
                                        Menu.FindItemByValue(dtResult.Rows[index]["ValueOwner"].ToString().Trim()).Visible = true;
                                    }
                                }
                                catch { }
                                try
                                {
                                    Menu.FindItemByValue(dtResult.Rows[index]["Value"].ToString().Trim()).Visible = true;
                                }
                                catch { }
                            }
                        }
                        else
                        {
                            if (objSessionManager.IsUser)
                            {
                                DataTable dtResult = GPSTrackingDBV2Custom.Data.DataRepository.MenuItemsProvider.GetByUsername(objSessionManager.CompanyID, HttpContext.Current.User.Identity.Name);

                                for (int index = 0; index < dtResult.Rows.Count; index++)
                                {
                                    try
                                    {
                                        if (dtResult.Rows[index]["ValueOwner"] == null)
                                        {
                                            Menu.FindItemByValue(dtResult.Rows[index]["Value"].ToString().Trim()).Text = dtResult.Rows[index]["Description"].ToString();
                                            Menu.FindItemByValue(dtResult.Rows[index]["Value"].ToString().Trim()).Visible = true;
                                        }
                                        else
                                        {
                                            Menu.FindItemByValue(dtResult.Rows[index]["ValueOwner"].ToString().Trim()).Visible = true;
                                        }
                                    }
                                    catch { }
                                    try
                                    {
                                        Menu.FindItemByValue(dtResult.Rows[index]["Value"].ToString().Trim()).Visible = true;
                                    }
                                    catch { }
                                }
                            }
                        }
                    }
                }
                else
                {
                    string CompanyID = string.Empty;
                    string login = string.Empty;

                    if (Request.Cookies["CompanyID"] != null) CompanyID = Request.Cookies["CompanyID"].Value;
                    if (Request.Cookies["Login"] != null) login = Request.Cookies["Login"].Value;

                    System.Web.Security.FormsAuthentication.SignOut();
                    Session.Abandon();

                    if (!String.IsNullOrEmpty(login))
                    {
                        login = "UserLogin";
                        if (!String.IsNullOrEmpty(CompanyID))
                            Response.Redirect("/" + login + ".aspx?CompanyID=" + CompanyID, true);
                        else
                            Response.Redirect("/" + login + ".aspx", true);
                    }
                    else
                        Response.Redirect("/UserLogin.aspx", true);   
                }
            }
        }

        protected void btnLoginOut_Click(object sender, EventArgs e)
        {
            string CompanyID = string.Empty;
            string login = string.Empty;

            if (Request.Cookies["CompanyID"] != null) CompanyID = Request.Cookies["CompanyID"].Value;
            if (Request.Cookies["Login"] != null) login = Request.Cookies["Login"].Value;
            
            System.Web.Security.FormsAuthentication.SignOut();
            Session.Abandon();

            if (!String.IsNullOrEmpty(CompanyID))
                Response.Redirect("/" + login + ".aspx?CompanyID=" + CompanyID, true);
            else
                Response.Redirect(Server.MapPath("../../" +login + ".aspx"), true);
            //Response.Redirect("/UserLogin.aspx");
        }
    }
}